/**
 * Copyright (c) 2015-2020, wss 王山山
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wss.conversion.auto.plugin;

import com.jfinal.config.Plugins;
import com.jfinal.log.JdkLoggerFactory;
import com.jfinal.log.Logger;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.wss.conversion.config.Database;

/**
 * 自动配置C3p0数据库连接池
 * @author wss
 *
 */
public class AutoConfigC3p0Plugin extends AutoConfigPlugin {
	private static final Logger LOG = new JdkLoggerFactory().getLogger(AutoConfigC3p0Plugin.class); 
	
	private C3p0Plugin c3p0Plugin;
	
	public AutoConfigC3p0Plugin(Plugins plugins) {
		super(plugins);
	}

	@Override
	public void config() {
		Database database = applicationConfig.getDatabase();
		if (database.isAviable()) {
			this.c3p0Plugin = new C3p0Plugin(database.getUrl(), database.getUser(), database.getPassword());
			c3p0Plugin.setDriverClass(database.getDriverClass());
			plugins.add(c3p0Plugin);
		}
		else {
			LOG.warn("Database configuration information is not complete, and can not enable the C3P0 database connection pool");
		}
	}
	
	public C3p0Plugin getC3p0Plugin() {
		return c3p0Plugin;
	}
}
